//
//  ViewController.m
//  14-CollectionViewTestAnswer
//
//  Created by 李昊林 on 16/8/2.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "ImageCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "LabelAndCollectionViewReusableView.h"
#define kCollectionViewReuse @"reuse"
#define kCollectionViewReusableViewHeader @"headerReuse"
#define kArcRandomColor [UIColor colorWithRed:arc4random()% 256 /256.0f green:arc4random()% 256 /256.0f blue:arc4random()% 256 /256.0f alpha:1];
#define kCollectionViewReusableViewHeaderNoCall @"header"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //[UIColor colorWithRed:arc4random()% 256 /256.0f green:arc4random()% 256 /256.0f blue:arc4random()% 256 /256.0f alpha:1];
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width =((CGRectGetWidth(self.view.bounds) - 10) / 3);
    //item大小
    flow.itemSize = CGSizeMake(width, width);
    //行间距
    flow.minimumLineSpacing = 5;
    //列间距
    flow.minimumInteritemSpacing = 0;
    //头大小
    flow.headerReferenceSize = CGSizeMake(0, 50);
    //创建CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    [collectionView registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableViewHeader];
    //2.自定义头视图
    [collectionView registerClass:[LabelAndCollectionViewReusableView class] forSupplementaryViewOfKind:UIViewControllerHierarchyInconsistencyException withReuseIdentifier:kCollectionViewReusableViewHeaderNoCall];
    //通过上边距实现视图添加（核心是UICollectionView本质是UIScrollView）
    collectionView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -300, self.view.bounds.size.width, 290)
                    ];
    view.backgroundColor = [UIColor greenColor];
    [collectionView addSubview:view];
//下面是return 100；
    //2.自定义头视图
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 100;
//    if (section == 0) {
//        return 0;
//    }
//    return 100;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.backgroundColor =kArcRandomColor;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return nil;
    } else {
        UICollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor yellowColor];
        return reuesableView;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    LabelCollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusableViewHeader forIndexPath:indexPath];
    //reuseableView.backgroundColor = [UIColor redColor];
    return reuseableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
